import React, { Component } from "react";
import { View, Text, Image, StyleSheet, TextInput, TouchableWithoutFeedback, FlatList } from "react-native";
import CustomerHeader from "../../widget/customerHeader";
import ScrollableTabView, { ScrollableTabBar,DefaultTabBar} from 'react-native-scrollable-tab-view';
import  LinearGradient from 'react-native-linear-gradient';
import PropTypes from "prop-types";

class MyFansCustomTabBar extends Component{
  constructor(props) {
    super(props);
  }
  static propTypes = {
    tabLabelNames: PropTypes.array, // tabs
    goToPage: PropTypes.func, // 跳转到对应tab的方法
    tabs: PropTypes.array,
    activeTab: PropTypes.number,
    onPress:PropTypes.func
  }
  render() {
    const containerWidth = this.props.containerWidth;
    const numberOfTabs = this.props.tabs.length;
    const translateX = this.props.scrollValue.interpolate({
      inputRange: [0, 1],
      outputRange: [0, containerWidth / numberOfTabs],
    });
    return (
      <View style={[this.props.style,styles.tabs]}>
        {this.props.tabs.map((tab,i)=>{
          return <TouchableWithoutFeedback style={styles.tabBox} key={tab} onPress={()=>this.props.goToPage(i)}>
            <View style={styles.tabBox}>
              <Text style={[styles.tab,i === this.props.activeTab?{fontSize:14,color:'#222222',fontWeight:'500'}:{}, this.props.textStyle]}>{this.props.tabLabelNames[i].label}</Text>
              {i===this.props.activeTab?<Image style={styles.lineBar}  source={require('../../img/line_bar.png')}></Image>:null}
            </View>
          </TouchableWithoutFeedback>
        })}
        <View style={styles.bottomLine}></View>
      </View>
    );
  }
}

export default class MyFansList extends Component{
  constructor(props) {
    super(props);
    this.state={
      tabList:[{
        label:'关注(13)',
        index:1
      },{
        label:'粉丝(23)',
        index:2
      }],
      dataList:[{
        label:'1',
        value:1
      },{
        label:'2',
        value:2
      }]
    }
  }
  onChangeTab(obj){
    console.log(obj)
  }
  selectTabChange(i){
    if(this.tabView)this.tabView.goToPage(i)
  }
  _renderItem(){
    return (
      <View style={styles.cell}>
        <Image style={styles.portrait} source={{uri:'https://cdn.uviewui.com/uview/swiper/swiper2.png'}}></Image>
        <Text style={styles.name}>兔子爱吃胡萝卜</Text>
        <LinearGradient colors={['#e79c58','#f8da85']} start={{x:0,y:1}} style={styles.btn}>
          <Text style={styles.btnTitle}>回关</Text>
        </LinearGradient>
      </View>
    )
  }
  render() {
    return(
      <View style={{flex:1,backgroundColor:'#fff'}}>
        <CustomerHeader style={styles.nav} showLine={false} title={'互关列表'} leftOnPress={()=>{this.props.navigation.pop()}}></CustomerHeader>
        <View style={{flex:1,backgroundColor:'#F6F6F6'}}>
          <View style={{flex:1}}>
            <ScrollableTabView initialPage={0} ref={(tabView) => { this.tabView = tabView; }} prerenderingsiblingsNumber={1} scrollWithoutAnimation={false} onChangeTab={this.onChangeTab} onScroll={
              (position) => {
                console.log('滑动时的位置：' + position);
              }
            } locked={false} renderTabBar={()=><MyFansCustomTabBar tabLabelNames={this.state.tabList} onPress={this.selectTabChange}/>}>
              <View tabLabel='Tab1' style={{flex: 1}}>
                <View style={{flex:1 ,width:'100%'}}>
                  <View style={{backgroundColor:'#fff',height:60}}>
                    <View style={styles.searchBox}>
                      <Image style={styles.searchIcon} source={require('../../img/search_icon.png')}></Image>
                      <TextInput style={styles.input} placeholder={'搜一搜'}></TextInput>
                    </View>
                  </View>
                  <FlatList data={this.state.dataList} renderItem={this._renderItem} keyExtractor={(item)=>item.value}></FlatList>
                </View>
              </View>
              <View tabLabel='Tab2' style={{flex: 1}}>
                <View style={{flex:1 ,width:'100%'}}>
                  <Text>界面4</Text>
                </View>
              </View>
            </ScrollableTabView>
          </View>
        </View>
      </View>
    )
  }
}

const styles=StyleSheet.create({
  nav:{
    backgroundColor:'rgba(255,255,255,1)',
  },
  searchBox:{
    flexDirection:'row',
    justifyContent:'flex-start',
    alignItems:'center',
    marginHorizontal:15,
    height:30,
    borderRadius:15,
    backgroundColor: '#F6F7F9',
    marginTop:15
  },
  searchIcon:{
    width:13,
    height:13,
    marginLeft:13
  },
  input:{
    marginLeft:7
  },
  cell:{
    height:73,
    backgroundColor:'#fff',
    flexDirection:'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    paddingHorizontal:15
  },
  portrait:{
    width:45,
    height:45,
    borderRadius:22.5
  },
  name:{
    color:'#000000',
    fontSize:15,
    marginLeft:9,
    flex:1
  },
  btn:{
    width:65,
    height:30,
    borderRadius:15
  },
  btnTitle:{
    color: '#fff',
    fontSize:12,
    flex:1,
    textAlign:'center',
    lineHeight:30
  },

  /**tab**/
  tabs:{
    height:40,
    backgroundColor:'#fff',
    flexDirection:'row',
    justifyContent:'space-around',
    alignItems:'center',
    paddingHorizontal:80,
  },
  tabBox:{
    height:40,
    justifyContent: 'center',
    alignItems:'center',
    flex:1
  },
  tab:{
    color: '#666666',
    fontSize: 14,
    paddingLeft: 10,
    paddingRight:10
  },
  lineBar:{
    position:'absolute',
    width:20,
    height:3,
    bottom:0,
  },
})

